package com.orangelife.mobile.property.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.jump.PropertyJumper;
import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.MoneyUtil;
import com.curry.orangelife.mobile.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 物业缴费-应缴账单明细-账单列表的适配器
 *
 * @author wgf
 * @since v3.6.5
 */
public class PropertyBillFeeDetailAdapter<T> extends FatherAdapter<T> {
    private Context mContext;
    private List<Map<String, Object>> mList;

    public PropertyBillFeeDetailAdapter(Context mContext, List<Map<String, Object>> list) {
        this.mContext = mContext;
        this.mList = list;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        mList = (List<Map<String, Object>>) list;
        return this;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_property_bill_detail, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.tvCostName, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tvCostName);
        }
        Map<String, Object> map = mList.get(position);
        holder.tvCostName.setText(String.valueOf(map.get("year")) + "年欠费");
        holder.tvCostContent.setText(MoneyUtil.handleMoney(String.valueOf(map.get("ytotal"))) + "元");
        holder.rlItem.setTag(R.id.tvCostContent, position);
        holder.rlItem.setOnClickListener(mListener);
        holder.tvSelectedMonth.setText(String.valueOf(map.get("yearSelectedInfo")));
        return convertView;
    }

    private View.OnClickListener mListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.rlItem:
                    int i = (int) v.getTag(R.id.tvCostContent);
                    jumpToMonthDetail(mList.get(i), canOperate(i), i);
                    break;
            }
        }
    };

    /** 如果上一个年全选，下一年全没选，则可以进行操作，如果是最后一个 */
    private boolean canOperate(int position) {
        if (mList.size() <= 1) {//只有一个年份就肯定可以处理
            return true;
        }
        if (position == 0) {//判断下个年全没选即可
            String nextYearStatus = String.valueOf(mList.get(1).get("yearStatus"));
            if ("2".equals(nextYearStatus)) {
                return true;
            }
        } else if (position == mList.size() - 1) {//判断上个年即可
            String preYearStatus = String.valueOf(mList.get(position - 1).get("yearStatus"));
            if ("1".equals(preYearStatus)) {
                return true;
            }
        } else {
            String nextYearStatus = String.valueOf(mList.get(position + 1).get("yearStatus"));
            String preYearStatus = String.valueOf(mList.get(position - 1).get("yearStatus"));
            String status = String.valueOf(mList.get(1).get("yearStatus"));
            if ("2".equals(nextYearStatus) && "1".equals(preYearStatus)) { //1 全选  2 全不选
                return true;
            }
        }
        return false;
    }

    private void jumpToMonthDetail(Map<String, Object> map, boolean canOperate, int position) {
        PropertyJumper.jumpToMonthDetail((Activity) mContext, (HashMap<String, Object>) map, canOperate, position);
    }

    static class ViewHolder {
        @Bind(R.id.tvCostName)
        TextView tvCostName;
        @Bind(R.id.tvCostContent)
        TextView tvCostContent;
        @Bind(R.id.rlItem)
        LinearLayout rlItem;
        @Bind(R.id.tvSelectedMonth)
        TextView tvSelectedMonth;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
